package com.htzz.collection.service;

import cn.hutool.core.util.ObjectUtil;
import com.htzz.collection.config.Constraint;
import com.htzz.collection.config.SystemConfig;
import com.htzz.collection.core.mongo.MongoTemplate;
import com.htzz.collection.domain.CollectionDataDto;
import com.htzz.collection.domain.CollectionStateDataDto;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
@Order(-1)
public class CollectionService {

    @Resource(name = "priMongo")
    private MongoTemplate template;


    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    public void dataAcquire(CollectionDataDto collectionDataDto) throws Exception{
//        Map<String,Object> rmap = new HashMap<>();
        String nowTime = sdf.format(new Date());
        List<Map<String, Object>> datas = collectionDataDto.getData();
        List<Document> dcs = new ArrayList<>();
        String devId = collectionDataDto.getDevId();
        for (Map<String, Object> data : datas) {

            if (ObjectUtil.isEmpty(data.get("k")) || ObjectUtil.isEmpty(data.get("v"))) {
                throw new Exception("采集点k和对应的值v不能为空");
            }

            String t = data.get("t") == null ? "0" : data.get("t").toString();
            if ("0".equals(t)) {
                data.put("t", nowTime);
            } else {
                Calendar instance = Calendar.getInstance();
                instance.setTimeInMillis(Long.parseLong(t));
                String tTime = sdf.format(instance.getTime());
                data.put("t", tTime);
            }
            data.put("createTime", nowTime);
            data.put("devId", devId);
            dcs.add(new Document(data));
        }
        template.insertMany("t"+devId,dcs);
//        rmap.put(Constraint.CODE,SystemConfig.SUCCESS.getCode());
//        rmap.put(Constraint.MSG,SystemConfig.SUCCESS.getMsg());
//        return rmap;
    }


    public void dataStateAcquire(CollectionStateDataDto collectionStateDataDto) throws Exception {
//        Map<String,Object> rmap = new HashMap<>();
        String nowTime = sdf.format(new Date());
        List<Map<String, Object>> datas = collectionStateDataDto.getData();
        Map<String,List<Document>> orgidatas = new HashMap<>();
        List<Document> dcs = new ArrayList<>();
        for (Map<String, Object> data : datas) {
            Object devId = data.get("devId");

            if (ObjectUtil.isEmpty(devId)) {
                throw new Exception("设备devId不能为空");
            }

            if (ObjectUtil.isEmpty(data.get("v"))) {
                throw new Exception("设备"+devId+"的状态值v不能为空");
            }

            Object t = data.get("t");
            if (null == t) {
                data.put("t", nowTime);
            } else {
                Calendar inst = Calendar.getInstance();
                inst.setTimeInMillis(Long.parseLong(t.toString()));
                String tTime = sdf.format(inst.getTime());
                data.put("t", tTime);
            }
            data.put("createTime", nowTime);
            data.put("k", "state");
            List<Document> documents = orgidatas.get(devId.toString());
            if (documents == null) {
                documents = new ArrayList<>();
            }
            documents.add(new Document(data));
            orgidatas.put(devId.toString(), documents);
        }
        for(Map.Entry entry:orgidatas.entrySet()){
            template.insertMany("t"+entry.getKey(),(List<Document>)entry.getValue());
        }
//        rmap.put(Constraint.CODE,SystemConfig.SUCCESS.getCode());
//        rmap.put(Constraint.MSG,SystemConfig.SUCCESS.getMsg());
//        return rmap;
    }

    /**
     * 存储数据
     * @param devId
     * @param data
     */
    public void saveData(String devId,Map<String, Object> data){
        List<Document> documents  = new ArrayList<>();
        documents.add(new Document(data));
        template.insertMany("t"+devId,documents);
    }

    /**
     * 保存数据
     * @param devId
     * @param datas
     */
    public void saveDatas(String devId,List<Map<String, Object>> datas){
        List<Document> documents  = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            documents.add(new Document(datas.get(i)));
        }
        template.insertMany("t"+devId,documents);
    }
}
